	//
	//  ViewController.m
	//  GCD常用函数
	//
	//  Created by 李煜 on 2024/3/20.
	//

#import "ViewController.h"
@interface ViewController ()

@end

@implementation ViewController

-(void)viewDidLoad {

	[super viewDidLoad];
	
//	[self invocationOperationWithQueue];
		   [self blockOperationWithQueue];
	
	/**
	 NSOperationQueue只有两种队列:
	 主队列和非主队列
	 主队列:[NSOperationQueue mainQueue]
	 非主队列:[[NSOperationQueue alloc]init]; (同时具备并发和串行的功能,默认是并发队列)
	 
	 操作步骤:
	 1. 创建操作
	 2. 创建队列
	 3. 添加操作到队列
	 */
}
- (void)invocationOperationWithQueue {
	NSInvocationOperation *op1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download1) object:nil];
	NSInvocationOperation *op2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download2) object:nil];
	NSInvocationOperation *op3 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download3) object:nil];
	NSOperationQueue *queue = [[NSOperationQueue alloc]init];
	
	[queue addOperation:op1];
	[queue addOperation:op2];
	[queue addOperation:op3];
		//内部已经调用start方法
	[queue addOperationWithBlock:^{
		NSLog(@"--download7--%@",[NSThread currentThread]);
	}];
}
- (void)blockOperationWithQueue {
	NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
		NSLog(@"===download1--%@",[NSThread currentThread]);
	}];
	NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
		NSLog(@"===download2--%@",[NSThread currentThread]);
	}];
	NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
		NSLog(@"===download3--%@",[NSThread currentThread]);
	}];
	[op2 addExecutionBlock:^{
		NSLog(@"===download4--%@",[NSThread currentThread]);
	}];
	[op2 addExecutionBlock:^{
		NSLog(@"===download5--%@",[NSThread currentThread]);
	}];
	[op2 addExecutionBlock:^{
		NSLog(@"===download6--%@",[NSThread currentThread]);
	}];
	NSOperationQueue *queue = [[NSOperationQueue alloc]init];
	
		//简便方法
		//1.创建操作2.添加操作到队列
	[queue addOperationWithBlock:^{
		NSLog(@"===download7--%@",[NSThread currentThread]);
	}];
	
	[queue addOperation:op1];
	[queue addOperation:op2];
	[queue addOperation:op3];
	
}
- (void)download1 { NSLog(@"--download1--%@",[NSThread currentThread]); }
- (void)download2 { NSLog(@"--download2--%@",[NSThread currentThread]); }
- (void)download3 { NSLog(@"--download3--%@",[NSThread currentThread]); }
@end
